Demo zu “Desktop Office Integration”

Demoprogramme SAP

Entwicklungsklasse: SOFFICEINTEGRATION

  • SAPRDEMO_PIVOTINTERFACE
  • SAPRDEMO_SPREADSHEET_INTERFACE
  • SAPRDEMO_TABLES_IN_EXCEL
  • SAPRDEMOEXCELINTEGRATION2

DOI-Demo von Black_Adept

Screenshot

* Darstellung Inplace auf Dynpro 9000 in einem Containercontrol namens
* “CONTAINER_9000”

* Minimale Fehlerbehandlung

REPORT zzdoidemo.
TYPE-POOLS: soi.

CONSTANTS: c_containername TYPE text20 VALUE 'CONTAINER_9000'.

* Definition der benötigten Controls
DATA: cl_container   TYPE REF TO cl_gui_custom_container,   " Container
cl_control     TYPE REF TO i_oi_container_control,"OIContainerCtrl
cl_document    TYPE REF TO i_oi_document_proxy,   "Office Dokument
cl_spreadsheet TYPE REF TO i_oi_spreadsheet.       " Spreadsheet

DATA: t_ranges       TYPE soi_range_list,
t_contents     TYPE soi_generic_table.

DATA: t_daten        TYPE STANDARD TABLE OF d010sinf    " Demodaten
WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 100,
ok_code        TYPE syucomm.  " Für Dynpro 9000

START-OF-SELECTION.
*** Demodaten selektieren
SELECT * UP TO 100 ROWS
INTO TABLE t_daten
FROM d010sinf
WHERE prog  LIKE 'Z%'.

END-OF-SELECTION.

* 1. Aufbau des Spreadsheet-Interfaces
* Version:
*  Leeres Blatt - alternativ kann man auch einen bestehenden
*  File vom Frontend oder aus dem BOR öffnen.
*  Kommentar an der relevanten Stelle in der folgenden Routine
  PERFORM build_spreadsheet_interface.

* Die Tabellenkalkulationsschnittstelle arbeitet mit benannten Bereichen
* Zur Demo werde ich einen n Zeilen, 5 Spalten großen Bereich
* definieren, in den ich nachher dann Daten einfülle
  PERFORM build_named_areas.

* Daten zur Verfügung stellen
  PERFORM set_data_into_sheet.

  CALL SCREEN 9000.

* Freigabe der Objekte
  FREE cl_spreadsheet.
CLEAR: cl_spreadsheet.

  IF NOT cl_document IS INITIAL.
CALL METHOD cl_document->release_document.
ENDIF.

  FREE:  cl_document,
cl_control,
cl_container.

  CLEAR: cl_document,
cl_control,
cl_container.

*&---------------------------------------------------------------------*
*&      Form  build_spreadsheet_interface
*&---------------------------------------------------------------------*
FORM build_spreadsheet_interface.

  TYPES: tyt_errors    TYPE STANDARD TABLE OF REF TO i_oi_error
WITH NON-UNIQUE DEFAULT KEY.

  DATA:  error         TYPE REF TO i_oi_error,
t_errors      TYPE tyt_errors,

         repid TYPE syrepid,
dynnr TYPE sydynnr.

* Container erzeugen
repid = sy-repid.
dynnr = '9000'.
CREATE OBJECT cl_container
EXPORTING
container_name              = c_containername
repid                       = repid
dynnr                       = dynnr
EXCEPTIONS
cntl_error                  = 1
cntl_system_error           = 2
create_error                = 3
lifetime_error              = 4
lifetime_dynpro_dynpro_link = 5
others                      = 6.
IF sy-subrc <> 0.
BREAK-POINT.STOP.
ENDIF.

************************************************************************
**  Hier geht die DOI-Erzeugung los
************************************************************************

* Instanz des OI-controls erzeugen
CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING
control = cl_control
error   = error.
APPEND error TO t_errors.

* control aufbauen
CALL METHOD cl_control->init_control
EXPORTING
inplace_enabled          = 'X'
no_flush                 = 'X'
*          INPLACE_SHOW_TOOLBARS    = ' '
r3_application_name      = 'Demo Document Container'
parent                   = cl_container
IMPORTING
error                    = error
EXCEPTIONS
*          javabeannotsupported     = 1  " ex. nicht im Minisap
OTHERS                   = 2.
IF sy-subrc <> 0.
BREAK-POINT.
STOP.
ENDIF.
APPEND error TO t_errors.

* Documentproxy holen
CALL METHOD cl_control->get_document_proxy
EXPORTING
document_type      = 'Excel.Sheet'                " EXCEL
*          document_type      = 'Word.Document'              " WORD
no_flush           = 'X'
IMPORTING
document_proxy     = cl_document
error              = error.
APPEND error TO t_errors.

* Leeres Dokument erzeugen,
CALL METHOD cl_document->create_document
EXPORTING
document_title   = 'Demo für Enno' " sichtbar wenn nicht inplace
no_flush         = 'X'
open_inplace     = 'X'
*    OPEN_READONLY    = ' '
IMPORTING
error            = error.
APPEND error TO t_errors.

* Alternativ: vorhandenes Dokument öffnen:
* Mit der Methode cl_document->open_document kann man ein schon
* vorhandenes Dokument ( etwa eine Vorlage öffnen ).
* Falls du eine lokale Datei öffnen willst: URL muss mit "'file://'"
* beginnen - dahinter dann gleich der Filename.  Beliebt ist auch
* eine URL aus dem BOR

* Und Spreadsheetinterface drüberlegen
CALL METHOD cl_document->get_spreadsheet_interface
EXPORTING
no_flush        = ' '
IMPORTING
error           =  error
sheet_interface = cl_spreadsheet.
APPEND error TO t_errors.
sy-subrc = 0.

  LOOP AT t_errors INTO error.
CALL METHOD error->raise_message
EXPORTING  type     = 'E'
EXCEPTIONS message_raised = 1
flush_failed   = 2.
ENDLOOP.

ENDFORM.                    " build_spreadsheet_interface

*&---------------------------------------------------------------------*
*&      Form  build_named_areas
*&---------------------------------------------------------------------*
FORM build_named_areas.

  DATA: error    TYPE REF TO i_oi_error,
rows     TYPE i,
columns  TYPE i.

  columns = 5.
DESCRIBE TABLE t_daten LINES rows.
* Datenbereich benennen
  CALL METHOD cl_spreadsheet->insert_range_dim
EXPORTING
no_flush  = 'X'  " Reicht bei nächstem Aufruf
name      = 'ALL'
left      = 2
top       = 8
rows      = rows
columns   = columns
IMPORTING
error     = error.

* Faul sein - und Rangestabelle zurückgeben lassen
CALL METHOD cl_spreadsheet->get_ranges_names
IMPORTING
error    = error
ranges   = t_ranges.

ENDFORM.                    " build_named_areas

*&---------------------------------------------------------------------*
*&      Form  set_data_into_sheet
*&---------------------------------------------------------------------*
FORM set_data_into_sheet.

  DATA: error      TYPE REF TO i_oi_error,
wa_range   LIKE LINE OF t_ranges,
wa_daten   LIKE LINE OF t_daten,
wa_content LIKE LINE OF t_contents.

* ACHTUNG - es müssen ALLE einträge in der Rangelist versorg werden
* und dort müssen auch ALLE Zellen ( auch wenn sie leer sind ) einmal
* auftauchen
  DELETE t_ranges WHERE NOT name = 'ALL'.
CLEAR t_contents.
LOOP AT t_ranges INTO wa_range.
CASE wa_range-name.
WHEN 'ALL'.
LOOP AT t_daten INTO wa_daten.
CLEAR wa_content.
* Row und Column beziehen sich auf die linke obere Ecke des Bereichs
wa_content-row = sy-tabix.
wa_content-column = 1.

          wa_content-value = wa_daten-prog.
APPEND wa_content TO t_contents. ADD 1 TO wa_content-column.

          wa_content-value = wa_daten-cnam.
APPEND wa_content TO t_contents. ADD 1 TO wa_content-column.

          WRITE wa_daten-cdat TO wa_content-value.
APPEND wa_content TO t_contents. ADD 1 TO wa_content-column.

          WRITE wa_daten-udat TO wa_content-value.
APPEND wa_content TO t_contents. ADD 1 TO wa_content-column.

          WRITE wa_daten-utime TO wa_content-value.
APPEND wa_content TO t_contents. ADD 1 TO wa_content-column.
ENDLOOP.

      WHEN OTHERS.
BREAK-POINT.STOP.  " sollte nicht vorkommen, wenn korrekt
ENDCASE.
ENDLOOP.

* ACHTUNG!  Formatierung VOR dem Setzen der Daten.  Sonst kommt es bei
* z.B. Datumsfeldern zu seltsamen Effekten.  Zum Testen die
* Formatierungsroutine mal HINTER die Datensetzenroutine setzen
* und staunen.
  DATA t_cells TYPE soi_cell_table.
DATA wa_cell LIKE LINE OF t_cells.

  wa_cell-top     = 10.
wa_cell-left    = 3.
wa_cell-rows    = 5.
wa_cell-columns = 2.
wa_cell-front   = 5. " Blau - siehe Doku in SAP-Bibliothek
wa_cell-back    = 27.
wa_cell-bold    = 1.
wa_cell-number  = 0.
wa_cell-font    = 'Courier New'.
wa_cell-size    = -1.  " Default
APPEND wa_cell TO t_cells.

* Und ein paar Zellen formatieren - Bereiche lassen sich mit einer
* anderen Methode schnell komplett formatieren.
CALL METHOD cl_spreadsheet->cell_format
EXPORTING
no_flush = 'X'
cells    = t_cells
IMPORTING
error    = error.

* Und jetzt die Daten in Spreadsheet schießen
CALL METHOD cl_spreadsheet->set_ranges_data
EXPORTING ranges    = t_ranges
contents  = t_contents
updating  = -1
no_flush  = 'X'
IMPORTING error     = error.

* Spaltenbreiten Optimieren
CALL METHOD cl_spreadsheet->fit_widest
EXPORTING
no_flush = ' '
name     = 'ALL'
IMPORTING
error    = error.

ENDFORM.                    " set_data_into_sheet

*&---------------------------------------------------------------------*
*& Form free_objects
*&---------------------------------------------------------------------*
FORM free_objects.

*** Freigabe der Objekte
  IF NOT cl_document IS INITIAL.
CALL METHOD cl_document->release_document.
CALL METHOD cl_document->close_document.
FREE cl_document.
ENDIF.

  IF NOT cl_control IS INITIAL.
CALL METHOD cl_control->destroy_control.
FREE cl_control.
ENDIF.

  FREE:  cl_spreadsheet.
CLEAR: cl_spreadsheet.
FREE:  cl_container.
CLEAR: cl_container.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.

  SET PF-STATUS '9000'.
SET TITLEBAR '9000'.

ENDMODULE.                 " STATUS_9000  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_9000 INPUT.

  CASE ok_code.
WHEN 'BACK'.
PERFORM free_objects.
SET SCREEN 0. LEAVE SCREEN.
ENDCASE.

ENDMODULE.                 " USER_COMMAND_9000  INPUT

Ablauflogik Dynpro 9000

PROCESS BEFORE OUTPUT.
MODULE STATUS_9000.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_9000.

Status ‘9000’


Der PF-Status vom Dynpro 9000
Enno Wulff

Leave a Comment